/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException.Argument;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.mailbox.MailSender;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mime.Mime.FixedMimeMessage;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.util.List;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMessage.RecipientType;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestMailSender
/*     */   extends TestCase
/*     */ {
/*     */   static final int TEST_SMTP_PORT = 6025;
/*  45 */   private static final String NAME_PREFIX = TestMailSender.class.getSimpleName();
/*     */   private static final String SENDER_NAME = "user1";
/*     */   private static final String RECIPIENT_NAME = "user2";
/*  48 */   private String mOriginalSmtpPort = null;
/*     */   private String mOriginalSmtpSendPartial;
/*     */   private String mOriginalAllowAnyFrom;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  55 */     this.mOriginalSmtpPort = Provisioning.getInstance().getLocalServer().getSmtpPortAsString();
/*  56 */     this.mOriginalSmtpSendPartial = TestUtil.getServerAttr("zimbraSmtpSendPartial");
/*  57 */     this.mOriginalAllowAnyFrom = TestUtil.getAccountAttr("user1", "zimbraAllowAnyFromAddress");
/*     */   }
/*     */   
/*     */   public void testRejectRecipient() throws Exception
/*     */   {
/*  62 */     String errorMsg = "Sender address rejected: User unknown in relay recipient table";
/*  63 */     String bogusAddress = TestUtil.getAddress("bogus");
/*  64 */     startDummySmtpServer(bogusAddress, errorMsg);
/*  65 */     Server server = Provisioning.getInstance().getLocalServer();
/*  66 */     server.setSmtpPort(6025);
/*     */     
/*  68 */     String content = TestUtil.getTestMessage(NAME_PREFIX + " testRejectSender", bogusAddress, "user1", null);
/*  69 */     MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
/*  70 */     Mailbox mbox = TestUtil.getMailbox("user1");
/*     */     
/*     */ 
/*  73 */     boolean sendFailed = false;
/*  74 */     server.setSmtpSendPartial(false);
/*     */     try {
/*  76 */       mbox.getMailSender().sendMimeMessage(null, mbox, msg);
/*     */     } catch (MailServiceException e) {
/*  78 */       validateException(e, "mail.SEND_ABORTED_ADDRESS_FAILURE", bogusAddress, errorMsg);
/*  79 */       sendFailed = true;
/*     */     }
/*  81 */     assertTrue(sendFailed);
/*     */     
/*     */ 
/*  84 */     startDummySmtpServer(bogusAddress, errorMsg);
/*  85 */     sendFailed = false;
/*  86 */     server.setSmtpSendPartial(true);
/*  87 */     MailSender sender = mbox.getMailSender().setSendPartial(false);
/*     */     try
/*     */     {
/*  90 */       sender.sendMimeMessage(null, mbox, msg);
/*     */     } catch (MailServiceException e) {
/*  92 */       validateException(e, "mail.SEND_ABORTED_ADDRESS_FAILURE", bogusAddress, errorMsg);
/*  93 */       sendFailed = true;
/*     */     }
/*  95 */     assertTrue(sendFailed);
/*     */     
/*     */ 
/*  98 */     startDummySmtpServer(bogusAddress, errorMsg);
/*  99 */     sendFailed = false;
/* 100 */     String validAddress = TestUtil.getAddress("user2");
/* 101 */     InternetAddress[] recipients = new InternetAddress[2];
/* 102 */     recipients[0] = new JavaMailInternetAddress(validAddress);
/* 103 */     recipients[1] = new JavaMailInternetAddress(bogusAddress);
/* 104 */     msg.setRecipients(MimeMessage.RecipientType.TO, recipients);
/* 105 */     server.setSmtpSendPartial(false);
/*     */     try {
/* 107 */       mbox.getMailSender().sendMimeMessage(null, mbox, msg);
/*     */     } catch (MailServiceException e) {
/* 109 */       validateException(e, "mail.SEND_ABORTED_ADDRESS_FAILURE", bogusAddress, errorMsg);
/* 110 */       sendFailed = true;
/*     */     }
/* 112 */     assertTrue(sendFailed);
/*     */     
/*     */ 
/* 115 */     startDummySmtpServer(bogusAddress, errorMsg);
/* 116 */     server.setSmtpSendPartial(true);
/* 117 */     sendFailed = false;
/*     */     try {
/* 119 */       mbox.getMailSender().sendMimeMessage(null, mbox, msg);
/*     */     } catch (MailServiceException e) {
/* 121 */       validateException(e, "mail.SEND_PARTIAL_ADDRESS_FAILURE", bogusAddress, null);
/* 122 */       sendFailed = true;
/*     */     }
/* 124 */     assertTrue(sendFailed);
/*     */     
/*     */ 
/* 127 */     server.setSmtpSendPartial(false);
/* 128 */     startDummySmtpServer(bogusAddress, errorMsg);
/* 129 */     sendFailed = false;
/* 130 */     sender = mbox.getMailSender().setSendPartial(true);
/*     */     try {
/* 132 */       sender.sendMimeMessage(null, mbox, msg);
/*     */     }
/*     */     catch (MailServiceException e)
/*     */     {
/* 136 */       validateException(e, "mail.SEND_PARTIAL_ADDRESS_FAILURE", bogusAddress, null);
/* 137 */       sendFailed = true;
/*     */     }
/* 139 */     assertTrue(sendFailed);
/*     */   }
/*     */   
/*     */   public void testRestrictEnvelopeSender() throws Exception
/*     */   {
/* 144 */     Server server = Provisioning.getInstance().getLocalServer();
/* 145 */     server.setSmtpPort(6025);
/*     */     
/* 147 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 148 */     Account account = mbox.getAccount();
/*     */     
/*     */ 
/* 151 */     String from = TestUtil.getAddress("testRestrictEnvelopeSender");
/* 152 */     String subject = NAME_PREFIX + " testRestrictEnvelopeSender";
/* 153 */     MessageBuilder builder = new MessageBuilder().withFrom(from).withToRecipient("user2").withSubject(subject).withBody("Who are you?");
/*     */     
/* 155 */     String content = builder.create();
/* 156 */     MimeMessage msg = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
/*     */     
/* 158 */     account.setSmtpRestrictEnvelopeFrom(true);
/*     */     
/*     */ 
/* 161 */     account.setAllowAnyFromAddress(false);
/* 162 */     DummySmtpServer smtp = startDummySmtpServer(null, null);
/* 163 */     mbox.getMailSender().sendMimeMessage(null, mbox, msg);
/* 164 */     assertEquals(account.getName(), smtp.getMailFrom());
/*     */     
/* 166 */     assertTrue(getHeaderValue(smtp.getDataLines(), "From").contains(account.getName()));
/*     */     
/*     */ 
/* 169 */     msg = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
/* 170 */     account.setAllowAnyFromAddress(true);
/* 171 */     smtp = startDummySmtpServer(null, null);
/* 172 */     mbox.getMailSender().sendMimeMessage(null, mbox, msg);
/* 173 */     assertEquals(account.getName(), smtp.getMailFrom());
/* 174 */     assertEquals(from, getHeaderValue(smtp.getDataLines(), "From"));
/*     */     
/* 176 */     account.setSmtpRestrictEnvelopeFrom(false);
/*     */     
/*     */ 
/* 179 */     account.setAllowAnyFromAddress(false);
/* 180 */     smtp = startDummySmtpServer(null, null);
/* 181 */     mbox.getMailSender().sendMimeMessage(null, mbox, msg);
/* 182 */     assertEquals(account.getName(), smtp.getMailFrom());
/* 183 */     assertTrue(getHeaderValue(smtp.getDataLines(), "From").contains(account.getName()));
/*     */     
/*     */ 
/* 186 */     msg = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
/* 187 */     account.setAllowAnyFromAddress(true);
/* 188 */     smtp = startDummySmtpServer(null, null);
/* 189 */     mbox.getMailSender().sendMimeMessage(null, mbox, msg);
/* 190 */     assertEquals(from, smtp.getMailFrom());
/* 191 */     assertEquals(from, getHeaderValue(smtp.getDataLines(), "From"));
/*     */   }
/*     */   
/*     */   private String getHeaderValue(List<String> dataLines, String headerName) {
/* 195 */     if (dataLines == null) {
/* 196 */       return null;
/*     */     }
/*     */     
/* 199 */     Pattern pat = Pattern.compile(headerName + ":\\s+(.*)");
/* 200 */     for (String line : dataLines) {
/* 201 */       Matcher m = pat.matcher(line);
/* 202 */       if (m.matches()) {
/* 203 */         return m.group(1);
/*     */       }
/*     */     }
/* 206 */     return null;
/*     */   }
/*     */   
/*     */   private DummySmtpServer startDummySmtpServer(String rejectedRecipient, String errorMsg) {
/* 210 */     DummySmtpServer smtp = new DummySmtpServer(6025);
/* 211 */     smtp.setRejectedRecipient(rejectedRecipient, errorMsg);
/* 212 */     Thread smtpServerThread = new Thread(smtp);
/* 213 */     smtpServerThread.start();
/* 214 */     return smtp;
/*     */   }
/*     */   
/*     */   private void validateException(MailServiceException e, String expectedCode, String invalidRecipient, String errorSubstring) {
/* 218 */     assertEquals(expectedCode, e.getCode());
/* 219 */     if (errorSubstring != null) {
/* 220 */       assertTrue("Error did not contain '" + errorSubstring + "': " + e.getMessage(), e.getMessage().contains(errorSubstring));
/*     */     }
/*     */     
/* 223 */     boolean foundRecipient = false;
/* 224 */     for (ServiceException.Argument arg : e.getArgs()) {
/* 225 */       if (arg.name.equals("invalid")) {
/* 226 */         assertEquals(invalidRecipient, arg.value);
/* 227 */         foundRecipient = true;
/*     */       }
/*     */     }
/* 230 */     assertTrue(foundRecipient);
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 236 */     cleanUp();
/* 237 */     TestUtil.setServerAttr("zimbraSmtpPort", this.mOriginalSmtpPort);
/* 238 */     TestUtil.setServerAttr("zimbraSmtpSendPartial", this.mOriginalSmtpSendPartial);
/* 239 */     TestUtil.setAccountAttr("user1", "zimbraAllowAnyFromAddress", this.mOriginalAllowAnyFrom);
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception {
/* 243 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 244 */     TestUtil.deleteTestData("user2", NAME_PREFIX);
/*     */   }
/*     */   
/*     */ 
/*     */   public static void main(String[] args)
/*     */     throws Exception
/*     */   {
/* 251 */     DummySmtpServer smtp = new DummySmtpServer(6025);
/* 252 */     if (args.length >= 2) {
/* 253 */       smtp.setRejectedRecipient(args[0], args[1]);
/*     */     }
/* 255 */     Thread thread = new Thread(smtp, DummySmtpServer.class.getSimpleName());
/* 256 */     thread.setDaemon(true);
/* 257 */     thread.start();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestMailSender.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */